//
//  DocCommentListViewController.m
//  安好
//
//  Created by lianchuang on 14/12/8.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "DocCommentListViewController.h"
#import "DocCommentInfoCell.h"
#import "DocCommentListCell.h"
#import "ConsultingChatViewController.h"
@interface DocCommentListViewController ()

@end

@implementation DocCommentListViewController
{
    NSMutableArray *commentsArray;
    NSDictionary *infoDic;
    NSInteger page;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    page=0;
    UIBarButtonItem *backImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backClicked)];
    backImg.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backImg;
    self.tableview=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-64) style:UITableViewStylePlain];
    [self.view addSubview:self.tableview];
    self.tableview.dataSource=self;
    self.tableview.delegate=self;
    self.tableview.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.tableview addFooterWithTarget:self action:@selector(loadCommenList)];

    self.navigationItem.title=@"医生评价";
    commentsArray=[[NSMutableArray alloc]init];
    infoDic=[[NSDictionary alloc]init];
    [self loadCommenList];
}
- (void)loadCommenList
{
    [self showActionView];
    page=page+1;
    NSString * postURL = [NSString stringWithFormat:@"%@docuser/commentlist/",kBaseURL];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *paramers=@{
                             @"platform": kPlatform,
                             @"version":kVersion,
                             @"doctoruid":self.doctoruid,
                             @"page":[NSString stringWithFormat:@"%ld",page],
                             };
    [manager POST:postURL parameters:paramers success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self hiddenActionView];
        [self.tableview footerEndRefreshing];
        NSString *status=[NSString stringWithFormat:@"%@",[responseObject valueForKey:@"status"]];
        if ([status isEqualToString:@"1"]) {
            if ([[[responseObject valueForKey:@"data"] valueForKey:@"data"] count]==0) {
            }else
            {
                for (int i=0; i<[[[responseObject valueForKey:@"data"] valueForKey:@"data"] count]; i++) {
                    [commentsArray addObject:[[[responseObject valueForKey:@"data"] valueForKey:@"data"] objectAtIndex:i]];
                }
            }
            infoDic=[[responseObject valueForKey:@"data"]valueForKey:@"score"];
            [self.tableview reloadData];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self hiddenActionView];
        [self.tableview footerEndRefreshing];
    }];

}
#pragma mark UItableviewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return 1;
    }else
    {
        return commentsArray.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        static NSString *indentifier=@"commentInfo";
        DocCommentInfoCell *infoCell=[tableView dequeueReusableCellWithIdentifier:indentifier];
        if (!infoCell) {
            if (kScreenWidth==414) {
                infoCell=[[[NSBundle mainBundle]loadNibNamed:@"DocCommentInfoCell" owner:self options:nil] lastObject];
            }else{
                infoCell=[[[NSBundle mainBundle]loadNibNamed:@"DocCommentInfoCell" owner:self options:nil] objectAtIndex:0];
            }
        }
        if (infoDic.count>0) {
            infoCell.whole.text=[NSString stringWithFormat:@"%@",[infoDic valueForKey:@"overall"]];
            infoCell.sever.text=[NSString stringWithFormat:@"%@",[infoDic valueForKey:@"attitude"]];
            infoCell.level.text=[NSString stringWithFormat:@"%@",[infoDic valueForKey:@"effect"]];
        
            float whole=[[NSString stringWithFormat:@"%@",[infoDic valueForKey:@"overall_c"]] floatValue];
            if (whole>=0) {
                infoCell.wholeImageView.image=[UIImage imageNamed:@"公用_上箭头.png"];
                infoCell.wholePeer.text=[NSString stringWithFormat:@"高于同行%.1f%%",whole*100];
                
            }else
            {
                infoCell.wholeImageView.image=[UIImage imageNamed:@"公用_下箭头"];
                infoCell.wholePeer.text=[NSString stringWithFormat:@"低于同行%.1f%%",whole*(-100)];
            }
            
            float sever=[[NSString stringWithFormat:@"%@",[infoDic valueForKey:@"attitude_c"]] floatValue];
            if (sever>=0) {
                infoCell.severImageView.image=[UIImage imageNamed:@"公用_上箭头"];
                infoCell.severPeer.text=[NSString stringWithFormat:@"高于同行%.1f%%",sever*100];
                
            }else
            {
                infoCell.severImageView.image=[UIImage imageNamed:@"公用_下箭头"];
                infoCell.severPeer.text=[NSString stringWithFormat:@"低于同行%.1f%%",sever*(-100)];
                
            }
            
            float level=[[NSString stringWithFormat:@"%@",[infoDic valueForKey:@"effect_c"]] floatValue];
            if (level>=0) {
                infoCell.levelImageView.image=[UIImage imageNamed:@"公用_上箭头"];
                infoCell.levelPeer.text=[NSString stringWithFormat:@"高于同行%.1f%%",level*100];
                
            }else
            {
                infoCell.levelImageView.image=[UIImage imageNamed:@"公用_下箭头"];
                infoCell.levelPeer.text=[NSString stringWithFormat:@"低于同行%.1f%%",level*(-100)];
            }

        }
        infoCell.selectionStyle=UITableViewCellSelectionStyleNone;
        return infoCell;
    }
    else
    {
        static NSString *inden=@"commentList";
        DocCommentListCell *listCell=[tableView dequeueReusableCellWithIdentifier:inden];
        if (!listCell) {
            listCell=[[[NSBundle mainBundle]loadNibNamed:@"DocCommentListCell" owner:self options:nil] lastObject];
        }
        if (commentsArray.count>0) {
            NSDictionary *dic=[commentsArray objectAtIndex:indexPath.row];
            listCell.personName.text=[dic valueForKey:@"username"];
            [listCell.personImage setImageWithURL:[NSURL URLWithString:[dic valueForKey:@"avatar"]] placeholderImage:[UIImage imageNamed:@"默认头像"]];
            listCell.personImage.layer.cornerRadius=3.0;
            listCell.personImage.layer.masksToBounds=YES;
            
            listCell.personComment.text=[[dic valueForKey:@"comment"]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
            [self autosizeFontAndSize:listCell.personComment withText:[dic valueForKey:@"comment"]];
            
            listCell.personDateline.text=[dic valueForKey:@"dateline"];
            listCell.personDateline.frame=CGRectMake(listCell.personDateline.left, listCell.personComment.bottom+10, listCell.personDateline.width, listCell.personDateline.height);
            listCell.personClock.frame=CGRectMake(listCell.personClock.left, listCell.personDateline.top, listCell.personClock.width, listCell.personClock.height);
            int count=[[dic valueForKey:@"star"] intValue];
            for (int i=0; i<count; i++) {
                CGFloat height;
                if (kScreenWidth==414) {
                    height=2;
                }
                UIImageView *iamgeview=(UIImageView *)[listCell viewWithTag:i+1];
                iamgeview.image=[UIImage imageNamed:@"评价_选中.png"];
            }
            listCell.bottomView.frame=CGRectMake(listCell.bottomView.left, listCell.personDateline.bottom+9.5, listCell.bottomView.width, 0.5);

        }
        listCell.selectionStyle=UITableViewCellSelectionStyleNone;
        return listCell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        if (kScreenWidth==414) {
            return 138;
        }else{
            return 115;
        }
    }
    else
    {
        DocCommentListCell *listCell=(DocCommentListCell *)[self tableView:self.tableview cellForRowAtIndexPath:indexPath];
        return listCell.personDateline.bottom+10;

    }
}
- (void)backClicked
{

    if (self.isFromDoc) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        //此处返回的是咨询详情页，如果你有另外的需求，请判断返回；
        for (UIViewController *chat in self.navigationController.viewControllers) {
            if ([chat isKindOfClass:[ConsultingChatViewController class]]) {
                [self.navigationController popToViewController:chat animated:YES];
            }
        }
    }
    
}
#pragma mark Lable自适应高度
- (void)autosizeFontAndSize:(UILabel *)label withText:(NSString *)text
{
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:label.text];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:5];//调整行间距
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [text length])];
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
    label.attributedText = attributedString;
    [label sizeToFit];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
